package com.lai.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.hospital.dao.HospitalDao;
import com.lai.hospital.entity.Hospital;
import com.lai.hospital.enums.FilePathEnum;
import com.lai.hospital.service.HospitalService;
import com.lai.hospital.strategy.context.UploadStrategyContext;
import com.lai.hospital.utils.BeanCopyUtils;
import com.lai.hospital.utils.PageUtils;
import com.lai.hospital.vo.ConditionVO;
import com.lai.hospital.vo.HospitalVO;
import com.lai.hospital.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.service.impl
 * @Description: 医院服务
 * @date Date : 2022年11月22日 14:37
 */
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalDao, Hospital> implements HospitalService {

    @Autowired
    private HospitalDao hospitalDao;

    @Autowired
    UploadStrategyContext uploadStrategyContext;


    @Override
    public PageResult<Hospital> listHospitalBack(ConditionVO conditionVO) {
        Integer count = hospitalDao.selectCount(null);
        List<Hospital> hospitalList = hospitalDao.listHospital(PageUtils.getLimitCurrent(),PageUtils.getSize(),conditionVO);
        return new PageResult<>(hospitalList,count);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateHospital(HospitalVO hospitalVO) {
        Hospital hospital = BeanCopyUtils.copyObject(hospitalVO, Hospital.class);
        hospitalDao.updateById(hospital);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String updateHospitalCover(MultipartFile file) {
        return uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.HOSPITAL.getPath());
    }

    @Override
    public Hospital getHospitalInfo() {
        return hospitalDao.selectOne(null);
    }
}
